package calculator;

import java.io.File;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import store.BaseStore;
import store.Cibo;
import store.Menu;
import store.Ricetta;
import dataview.Cibi;
import dataview.Data;
import dataview.EasyTextFile;
import dataview.Menus;
import dataview.Ricette;
import exceptions.DBException;
import exceptions.XmlException;

public class EsportaImporta {
	public static String EXPORT_DIR = System.getProperty("user.home") + "/easycooking/export/";
	private static final String ROOT_NAME = "easycooking-data";
	
	public static EasyTextFile esporta(Cibi cibi, Ricette ricette, Menus menus) throws DBException {
		Element root = new Element(ROOT_NAME);
		Document document = new Document(root);
		Element cc = new Element("cibi");
		Element rr = new Element("ricette");
		Element mm = new Element("menus");
		if (cibi != null) for (Cibo cibo : cibi)
			cc.addContent(cibo.toXml());
		if (ricette != null) for (Ricetta ricetta : ricette)
			rr.addContent(ricetta.toXml());
		if (menus != null) for (Menu menu : menus)
			mm.addContent(menu.toXml());
		if (!cc.getChildren().isEmpty()) root.addContent(cc);
		if (!rr.getChildren().isEmpty()) root.addContent(rr);
		if (!mm.getChildren().isEmpty()) root.addContent(mm);
		return new EasyTextFile(EXPORT_DIR + Data.toReportFormat() + ".ecd", new XMLOutputter(Format.getPrettyFormat()).outputString(document));
	}
	
	public static boolean controllaEC(File textfile) {
		Document document;
		try {
			document = getXmlDocument(textfile);
			if (document.getRootElement().getName() != ROOT_NAME) return false;
			BaseStore.checkValidation(document);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	/**
	 * Estre i cibi salvati nel file di testo textfile (viene assunto che il file e' del tipo
	 * corretto e che quindi non dovrebbe sollevare eccezioni)
	 * 
	 * @param textfile File da cui importare i cibi
	 * @return La lista di cibi contenuti nel file passato come parametro
	 * @throws DBException
	 * @throws XmlException
	 */
	public static Cibi estraiCibi(File textfile) throws DBException, XmlException {
		Cibi cibi = new Cibi();
		Element cc = getXmlDocument(textfile).getRootElement().getChild("cibi");
		if (cc != null) for (Object cibo : cc.getChildren())
			cibi.add(Cibo.fromXml((Element)cibo));
		return cibi;
	}
	
	public static Ricette estraiRicette(File textfile, Cibi cibiEstratti) throws DBException, XmlException {
		Ricette ricette = new Ricette();
		Element rr = getXmlDocument(textfile).getRootElement().getChild("ricette");
		if (rr != null) for (Object ricetta : rr.getChildren())
			ricette.add(Ricetta.fromXml((Element)ricetta, false));
		return ricette;
	}
	
	public static Menus estraiMenu(File textfile, Cibi cibiEstratti, Ricette ricetteEstratte) throws DBException, XmlException {
		Menus menus = new Menus();
		Element mm = getXmlDocument(textfile).getRootElement().getChild("menus");
		if (mm != null) for (Object menu : mm.getChildren())
			menus.add(Menu.fromXml((Element)menu, false));
		return menus;
	}
	
	private static Document getXmlDocument(File textfile) throws DBException {
		try {
			return new SAXBuilder().build(textfile);
		} catch (Exception e) {
			throw new DBException(e);
		}
	}
}
